package com.zscat.mallplus.qsg.contrller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgHexiao;
import com.zscat.mallplus.qsg.QsgRoom;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.qsg.service.IQsgHexiaoService;
import com.zscat.mallplus.qsg.service.IRoomService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@RestController
@Api(tags = "HexiaoController", description = "核销管理")
@RequestMapping("/admin/qsg/hexiao")
public class HexiaoController {

    @Autowired
    private IQsgHexiaoService hexiaoService;

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有核销列表")
    @ApiOperation("根据条件查询所有核销列表")
    @GetMapping(value = "/list")
    public Object getRoomByPage(@RequestParam(required = false) String code, @RequestParam(required = false) String memberPhone,
                                @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id", UserUtils.getCurrentMember().getQsgStoreId());
        if (StringUtils.isNotBlank(endTime)) {
            qw.le("create_datetime", endTime);
        }
        if (StringUtils.isNotBlank(startTime)) {
            qw.ge("create_datetime", startTime);
        }
        if (StringUtils.isNotBlank(code)){
            qw.eq("code",code);
        }
        if (StringUtils.isNotBlank(memberPhone)){
            qw.eq("member_phone",memberPhone);
        }
        try {
            IPage<QsgHexiao> hexiaoIPage = hexiaoService.page(new Page<>(pageNum, pageSize), qw);
            return new CommonResult().success(hexiaoIPage);
        } catch (Exception e) {
            log.error("根据条件查询所有核销列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }
}
